using System;
using System.Drawing;
namespace Circus.CodeEditor
{
	[Serializable]
    public partial class Range : ICloneable
	{
		private Point startPoint;
		private Point endPoint;
		public virtual Point StartPoint
		{
			get
			{
				return this.startPoint;
			}
			set
			{
				if (this.startPoint != value)
				{
					this.startPoint = value;
					this.OnStartPointChanged();
				}
			}
		}
		public virtual Point EndPoint
		{
			get
			{
				return this.endPoint;
			}
			set
			{
				if (this.endPoint != value)
				{
					this.endPoint = value;
					this.OnEndPointChanged();
				}
			}
		}
		public virtual Size Size
		{
			get
			{
				return new Size(this.endPoint.X - this.startPoint.X, this.endPoint.Y - this.startPoint.Y);
			}
			set
			{
				if (this.Size != value)
				{
					this.endPoint.X = this.startPoint.X + value.Width;
					this.endPoint.Y = this.startPoint.Y + value.Height;
					this.OnEndPointChanged();
				}
			}
		}
		public virtual bool IsEmpty
		{
			get
			{
				return this.startPoint.Y > this.endPoint.Y || (this.startPoint.Y == this.endPoint.Y && this.startPoint.X >= this.endPoint.X);
			}
		}
		protected virtual void OnStartPointChanged()
		{
		}
		protected virtual void OnEndPointChanged()
		{
		}
		public static bool InsideRange(Point pt, Rectangle rect)
		{
			return Range.InsideRange(pt, rect, false);
		}
		public static bool InsideRange(Point pt, Rectangle rect, bool checkMaxInt)
		{
			bool result;
			if (pt.Y <= rect.Bottom && pt.Y >= rect.Top)
			{
				if (pt.Y == rect.Top)
				{
					if (pt.Y == rect.Bottom)
					{
						result = (pt.X >= rect.Left && (pt.X < rect.Right || (checkMaxInt && pt.X == rect.Right && pt.X == int.MaxValue)));
					}
					else
					{
						result = (pt.X >= rect.Left);
					}
				}
				else
				{
					result = (pt.Y != rect.Bottom || pt.X < rect.Right || (checkMaxInt && pt.X == rect.Right && pt.X == int.MaxValue));
				}
			}
			else
			{
				result = false;
			}
			return result;
		}
		public static bool UpdatePos(int x, int y, int deltaX, int deltaY, ref Point pt, bool endPos)
		{
			int x2 = pt.X;
			int y2 = pt.Y;
			bool result = Range.UpdatePos(x, y, deltaX, deltaY, ref x2, ref y2, endPos);
			pt.X = x2;
			pt.Y = y2;
			return result;
		}
		public static bool UpdatePos(int x, int y, int deltaX, int deltaY, ref int ch, ref int ln, bool endPos)
		{
			int num = ch;
			int num2 = ln;
			if (deltaY == 0)
			{
				bool flag = deltaX < 0 || endPos;
				if (ln == y && ((ch >= x && !flag) || (ch > x && flag)))
				{
					if (ch != int.MaxValue)
					{
						ch += deltaX;
					}
					ch = Math.Max(ch, 0);
				}
			}
			else
			{
				if (ln > y || (ln == y && ((ch >= x && !endPos) || (ch > x && endPos))))
				{
					if ((ln == y && deltaY >= 0) || (ln + deltaY == y && deltaY < 0))
					{
						if (ch != int.MaxValue)
						{
							ch += deltaX;
						}
						ch = Math.Max(ch, 0);
					}
					if (ln != int.MaxValue)
					{
						ln += deltaY;
					}
					ln = Math.Max(ln, 0);
				}
			}
			return ch != num || ln != num2;
		}
	
		public Range(Point startPoint, Point endPoint)
		{
			this.startPoint = startPoint;
			this.endPoint = endPoint;
		}
		public Range(int x1, int y1, int x2, int y2)
		{
			this.startPoint = new Point(x1, y1);
			this.endPoint = new Point(x2, y2);
		}
		public bool ShouldSerializeSize()
		{
			return false;
		}
		public virtual void Assign(Range source)
		{
			this.StartPoint = source.StartPoint;
			this.EndPoint = source.EndPoint;
		}
		public virtual object Clone()
		{
			Range range = new Range();
			range.Assign(this);
			return range;
		}
	}
}
